//
//  FundDataViewController.m
//  Article
//
//  Created by Haricot_cpc17 on 16/4/22.
//  Copyright © 2016年 mac. All rights reserved.
//
//资金日志
#import "FundDataViewController.h"
#import "FundDataView.h"
#import "AllViewController.h"
#import "WaitViewController.h"
#import "DidViewController.h"

@interface FundDataViewController () <FundDataViewDelegate,UIScrollViewDelegate>

//头部的选项卡
@property(nonatomic,strong) FundDataView *titleView;

//滚动条
@property(nonatomic,strong) UIScrollView *scrollView;

//大数组，子控制器的
@property(nonatomic,strong) NSMutableArray *childViews;

@end

@implementation FundDataViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    //添加头部的view
    [self titleView];
    
    //添加scrollView
    [self scrollView];
    
    //添加各子控制器
    [self setupChildVcs];
    
    self.navigationItem.title = @"我的佣金";
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    // Do any additional setup after loading the view.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSMutableArray *)childViews
{
    if(_childViews==nil)
    {
        _childViews =[NSMutableArray array];
    }
    return _childViews;
}

- (UIScrollView *)scrollView
{
    if(_scrollView==nil)
    {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _titleView.frame.size.height, WIDTH, HEIGHT - _titleView.frame.size.height)];
        _scrollView.delegate=self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.contentSize = CGSizeMake(3 * WIDTH, 0);
        _scrollView.bounces = NO;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}


- (FundDataView *)titleView
{
    if(_titleView==nil)
    {
        _titleView =[FundDataView new];
        _titleView.layer.borderColor = [UIColorFromRGB(0xf2f2f2) CGColor];
        _titleView.layer.borderWidth = 1;
        _titleView.delegate=self;
        _titleView.frame = CGRectMake(0, 0, self.view.frame.size.width, 35);
        [self.view addSubview:_titleView];
        
    }
    return _titleView;
}

#pragma mark 添加各子控制器
- (void)setupChildVcs
{
    //全部控制器
    AllViewController *AllVc =[AllViewController new];
    [self addChildViewController:AllVc];
    [self.childViews addObject:AllVc.view];
    
    //一级控制器
    WaitViewController *WaiteVc =[WaitViewController new];
    [self addChildViewController:WaiteVc];
    [self.childViews addObject:WaiteVc.view];
    
    //二级控制器
    DidViewController *DidVc =[DidViewController new];
    [self addChildViewController:DidVc];
    [self.childViews addObject:DidVc.view];
    
    //三级控制器
//    ThirdViewController *MVc =[ThirdViewController new];
//    [self addChildViewController:MVc];
//    [self.childViews addObject:MVc.view];
    
    
    
    for(int i=0;i<self.childViews.count;i++)
    {
        UIView *childV = self.childViews[i];
        CGFloat childVX = WIDTH * i ;
        childV.frame = CGRectMake(childVX, 0, WIDTH, self.view.frame.size.height - _titleView.frame.size.height);
        [_scrollView addSubview:childV];
        
    }
    
    
}


#pragma mark 滚动条
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    [self.view endEditing:YES];
    
    
    if(scrollView==_scrollView)
    {
        if(_scrollView.contentOffset.x / WIDTH ==0)
        {
            [_titleView wanerSelected:0];
        }
        else if (_scrollView.contentOffset.x / WIDTH ==1)
        {
            [_titleView wanerSelected:1];
        }else if (_scrollView.contentOffset.x / WIDTH ==2)
        {
            [_titleView wanerSelected:2];
        }
    }
}

#pragma mark titleView的方法
- (void)titleView:(FundDataView *)titleView scollToIndex:(NSInteger)tagIndex
{
    [_scrollView setContentOffset:CGPointMake(tagIndex * WIDTH, 0) animated:YES];
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
